<template>
  <div class="container">
    <div class="mainContent">
      <div class="inputArea">
        <div class="singleInput">
          <div class="label">原始密码</div>
          <input type="text" v-model="old_pwd" placeholder="请输入原始密码" placeholder-style="color: #C7C7C7;" >
        </div>
        <div class="singleInput">
          <div class="label">新密码</div>
          <input type="password" v-model="new_pwd" placeholder="请输入新密码" placeholder-style="color: #C7C7C7;" >
        </div>
        <div class="singleInput">
          <div class="label">确认新密码</div>
          <input type="password" v-model="confirm_pwd" placeholder="请再次输入新密码" placeholder-style="color: #C7C7C7;" >
        </div>
      </div>
      <div class="btn" @click="changePwd">修改密码</div>
    </div>
  </div>
</template>

<script>
import JSEncrypt from 'jsencrypt/bin/jsencrypt'
export default {
  data () {
    return {
      old_pwd: '',
      new_pwd: '',
      confirm_pwd: ''
    }
  },
  methods: {
    changePwd () {
      if (this.new_pwd.length < 6 || this.new_pwd.length > 16) {
        wx.showToast({
          title: '请输入6~16位新密码',
          icon: 'none'
        })
      } else if (this.old_pwd === this.new_pwd) {
        wx.showToast({
          title: '新密码不能与原始密码相同',
          icon: 'none'
        })
      } else if (this.new_pwd !== this.confirm_pwd) {
        wx.showToast({
          title: '新密码两次输入不一致',
          icon: 'none'
        })
      } else {
        let encryptor = new JSEncrypt() // 新建JSEncrypt对象
        let publicKey =
          `MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCCU+sOs2dwS5k10e44LhAwEVDicF2SM9O6/cDdXuDyemjx/RMT/yDJHQ4+jqJOB11nIdeo2lvrD/9oPMePauTjCaXd7AxO75Gzx24skFOb6Rw5Tvjffgsm0ka+/UbK6OWdruPXJDSCrnRskQVhMNRrV6qGLaOCWCQzNwOcz7aCMQIDAQAB`
        encryptor.setPublicKey(publicKey) // 设置公钥
        let regTime = new Date().getTime()
        let oldPwd = encryptor.encrypt(regTime + this.old_pwd) // 对需要加密的数据进行加密
        let rsaPassWord = encryptor.encrypt(regTime + this.confirm_pwd) // 对需要加密的数据进行加密

        this.$httpWX.post({
          url: '/shengxueApi/identity/teacherModifyPassword',
          data: {
            credentials: oldPwd,
            modifyTime: regTime,
            password: rsaPassWord
          },
          header: {
            'content-type': 'application/json', // 默认值
            'Api-Version': '2.0.1',
            'accessToken': wx.getStorageSync('tokens')
          }
        }).then(res => {
          if (res.code === '0') {
            this.old_pwd = ''
            this.new_pwd = ''
            this.confirm_pwd = ''
            wx.showToast({
              title: '修改成功',
              icon: 'success'
            })
            wx.reLaunch({ url: '../index/main' })
          } else {
            wx.showToast({
              title: res.message,
              icon: 'none'
            })
          }
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .mainContent {
    padding: 0 32rpx;
    .inputArea {
      .singleInput {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        border-bottom: 1rpx solid #E5E8ED;
        height: 112rpx;
        width: 100%;
        .label {
          color: #222;
          font-size: 28rpx;
          line-height: 40rpx;
          width: 150rpx;
          margin-right: 24rpx;
        }
        input {
          flex: 1;
          height: 100%;
          line-height: 40rpx;
          color: #222;
          font-size: 26rpx;
        }
      }
    }
    .btn {
      margin: 0 auto;
      width: 622rpx;
      height: 88rpx;
      line-height: 88rpx;
      border-radius: 44rpx;
      background: #FFAD0D;
      color: #fff;
      font-size: 30rpx;
      font-weight: bold;
      text-align: center;
      letter-spacing: 2rpx;
      margin-top: 64rpx;
    }
  }
</style>
